package ipt.dao.F1;

import ipt.exp.AlumnoException;
import ipt.vo.Alumno;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
/**
 * Clase para realizar las pruebas de ProbarAlumnoTxt
 * @author alumno2
 *
 */
public class ProbarAlumnoTxt {

	//private static final String DRIVER_CLASS_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String CONNECTION_URL = "jdbc:odbc:PruebasCurso";
	private static final String DB_USER = "";
	private static final String DB_PWD = "";
	private final static String TABLE_NAME = "ALUMNOS";

	/**
	 * Metodo proncipal para leer un fichero de texto, guardar los campos en 
	 * un ArrayList de objetos alumno y despues hacer una INSERT
	 * en la base de datos PruebasAlumnos en la tabla Alumnos
	 * de los 10000 alumnos del fichero.
	 * @param args
	 * @throws AlumnoException
	 * @author alumno2
	 */
	
	public static void main(String[] args) throws AlumnoException {
		long time_start, time_end;
		Alumno al = new Alumno();
		AlumnoTXTDAO dao = new AlumnoTXTDAO();
		Connection conn = null;
		try {

			conn = DriverManager.getConnection(CONNECTION_URL, DB_USER, DB_PWD);
			conn.setAutoCommit(false);// Poner Autocommit a false

			Statement statement = conn.createStatement();
			int updateQuery = 0;
			int cont = 0;
			List<Alumno> lista = dao.getAlumnos();
			Iterator<Alumno> it = lista.iterator();
			time_start = System.currentTimeMillis();
			while (it.hasNext()) {
				al = it.next();
				
				int rol=0;
				switch (al.getRol().toString().charAt(0)) {
				case 'T':
					rol = 0;
					break;
				case 'J':
					rol = 1;
					break;
				case 'C':
					rol = 2;
					break;
				}

				String queryString = "INSERT INTO "
						+ TABLE_NAME
						+ " (DNI, NOMBRE, APELLIDO1, APELLIDO2, EDAD, ROL, EMAIL)VALUES ("
						+ "'" + al.getDni() + "', '" + al.getNombre() + "', '"
						+ al.getApellido1() + "', '" + al.getApellido2()
						+ "', " + al.getEdad() + ", " + rol + ", " + "'"
						+ al.getEmail() + "')";

				updateQuery = statement.executeUpdate(queryString);
				

				if (updateQuery == 1) {
					cont++;
					System.out.println("Correctas:" + cont+"rol "+rol+"char"+al.getRol().toString().charAt(0));
				}
				al = null;
			}
			conn.commit();
			System.out.println("Transaction commit...");
			
			System.out.println("Comitados:" + cont + " alumnos");
			
			time_end = System.currentTimeMillis();
			System.out.println("\nLa tarea ha tardado "
					+ (time_end - time_start) + " milisegundos ->"
					+ ((time_end - time_start) / 1000) + " segundos y "
					+ ((time_end - time_start) % 1000) + " milisegundos");

		} catch (SQLException e) {
			if (conn != null) {
				// En caso de Fallo
				try {
					conn.rollback();
				} catch (SQLException e1) {
					System.out.println(e.getMessage());
				}
				System.out.println("Connection rollback...");
			}
			System.out.println(e.getMessage());
			
		} catch (Exception e) {
			if (conn != null) {
				// En caso de Fallo
				try {
					conn.rollback();
				} catch (SQLException e1) {
					System.out.println(e.getMessage());
				}
				System.out.println("Connection rollback...");
			}
			System.out.println(e.getMessage());

		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}

	}
}
